﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XXN.Model;
using XXN.Model.Model;

namespace XXN.Service
{
  public class IimgInfoService
    {
        Shopping_MallEntities db = new Shopping_MallEntities();
        public List<IimgInfoModel> GetIimgInfo(int page, int limit, out int count)
        {
            var pro = from item in db.IimgInfoes.Where(p => true)
                      select new IimgInfoModel
                      {
                          Img_id = item.Img_id,
                          Url = item.Url,
                          Selected = item.Selected,
                          Pro_id = item.Pro_id,
                          Pro_id_name = item .Product .Pro_name 
                      };
            count = pro.Count();
            pro = pro.OrderBy(p => p.Img_id );
            pro = pro.Skip((page - 1) * limit).Take(limit);
            return pro.ToList();
        }
        public int AddInfo(IimgInfo img)
        {
            db.IimgInfoes.Add(img);
            return db.SaveChanges();
        }
        public IimgInfoModel EditInfo(int id)
        {
            var img = from item in db.IimgInfoes.Where(p => p.Img_id ==id )
                      select new IimgInfoModel
                      {
                          Img_id = item.Img_id,
                          Url = item.Url,
                          Selected = item.Selected,
                          Pro_id = item.Pro_id,
                          Pro_id_name = item.Product.Pro_name
                      };
            return img.SingleOrDefault();
        }
        public int EditInfo(IimgInfo img)
        {
            db.Entry(img).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
        public List<Product> GetInfos()
        {
            return db.Products.ToList();
        }
        public int DelInfo(int img_id)
        {
            var w=db.IimgInfoes.SingleOrDefault(x => x.Img_id == img_id);
            db.IimgInfoes.Remove(w);
            return db.SaveChanges();
        }
        public List<Product> GetIimgInfoTypeInfo()
        {
            return db.Products.ToList();
        }
    }
}
